<?php

namespace app\crm\controller\clues;

use app\common\controller\CrmController;
use app\common\service\BaseService;
use app\crm\model\CrmClues;
use think\Db;
use Exception;
use app\crm\traits\Curd;

class Pool extends CrmController
{
    protected $validate = 'clues\Clues';   //线索表验证器
    protected $model = 'CrmClues';          //线索数据模型  


    use Curd;

    public function initialize()
    {
        parent::initialize();
        if (in_array($this->request->action(), ['add', 'edit', 'index', 'recycle'])) {
            $this->assign('campusList', BaseService::getCampusList());  // 校区
            $this->assign('courseList', BaseService::getCourseList());  // 课程 
            $this->assign('sourceList', BaseService::getSourceList());  // 来源 


        }
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $filter_w['owner_user_id'] = 0;
            $where[] =  ['create_user_id', '=', session('admin.id')];
            $count = $this->model::with('course,campus,createUser')
                ->where($where)
                ->where($filter_w)
                ->count();
            $list = $this->model::with('course,campus,createUser')
                ->where($where)
                ->where($filter_w)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

  
    /**
     * 领取
     */
    public function receive()
    {
        $ids = $this->request->param('id');
        if (empty($ids)) {
            return $this->error('缺少id参数');
        }
       
        $success_num = 0;//成功领取的数量
        $error_num = 0;//失败数量
        $error_ids = "";//失败的ids
        $msg = "";//返回消息

        Db::startTrans();
        try {
            $list = $this->model->where('id', 'in', $ids)->lock(true)->select();
            if (!$list) {
                throw new Exception('无效的请求参数id');
            }
            foreach ($list as $row) {
                if ($row['owner_user_id']) {
                    $error_num++;
                    $error_ids = $error_ids ? $error_ids . "," . $row->id : $row->id;
                    continue;
                }
                
                $row->owner_user_id = session('admin.id');
                $row->follow_time = $row->collect_time = time();
                if($row->save()){
                    $success_num++;
                }
            }   
            Db::commit(); 
            $msg = "成功领取{$success_num}条\n失败{$error_num}\n失败ids:{$error_ids}";
            return ['msg'=>$msg,'code'=>1];
        } catch (\Throwable $th) {
            Db::rollback();
            return ['msg'=>'领取失败','code'=>0];
        }
    }

}
